package com.citywithincity.db;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.w3c.dom.Element;

import com.damai.utils.LogUtil;
import com.damai.xml.XMLUtil;
import com.damai.xml.XMLVisitor;

public class TableBindConfig {
	
	public static final Map<String, TableBindInfo> MAP = new ConcurrentHashMap<String, TableBindInfo>();
	
	public static void load(){
		try {
			XMLUtil.loadXmlLevel1("table.xml", new XMLVisitor() {
				
				@Override
				public void onVisit(Element element) {
					String table = element.getAttribute("table");
					int db=Integer.parseInt(element.getAttribute("db"));
					String pk = element.getAttribute("pk");
					boolean prefix = Boolean.parseBoolean(element.getAttribute("prefix"));
					String className = element.getAttribute("className");
					TableBindInfo bindInfo = new TableBindInfo(table, db, pk, prefix);
					MAP.put(className, bindInfo);
				}
			});
		} catch (IOException e) {
			
			LogUtil.log("Cannot find table.xml");
			
		}
	}
	
	public static TableBindInfo geTableBindInfo(Class<?> clazz){
		return MAP.get(clazz.getName());
	}
}
